﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace FranceTriple.Dal
{
    public class ShoutboxDal
    {
        public static IEnumerable<KeyValuePair<Int32, String>> GetMessages(Int32 MaxNumberOfMessage, String requesterTimeZone)
        {
            List<KeyValuePair<Int32, String>> result = new List<KeyValuePair<Int32, String>>();
            try
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {

                    var query = (from msg in ctx.ShoutBoxSet
                                 orderby msg.CreationDateShoutBox descending
                                 select msg).Take(MaxNumberOfMessage);
                    
                    foreach (var message in query)
                    {
                        result.Add(new KeyValuePair<Int32, String>(
                            message.Id,
                            String.Format("<p class=\"sbm\"><u><b>{0}:</b></u> {1}<br />{2}<br /></p><br />",
                                message.UserNameAuthorShoutBox,
                                ShoutboxDal.ReplaceSmiliesTextToImage(message.MessageShoutBox),
                                message.CreationDateShoutBox.ToUserTimeZone(requesterTimeZone))
                        ));
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("RetrieveError".ToLocalizedString(), "ShoutBox", ex.Message), ex) { EntityType = typeof(ShoutBox) };
            }
            return result;
        }

        public static string GetSmilies(String ScriptName)
        {
            StringBuilder sb;
            try
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    sb = new StringBuilder();
                    var query = (from sml in ctx.SmilieSet
                                 select sml);
                    foreach (Smilie sml in query)
                    {
                        sb.AppendLine(String.Format("<span style=\"cursor: hand;\" onclick=\"" + ScriptName + "(' " + sml.CodeSmilie + " ','Message');\">" +
                            "<img src=\"{0}\" border=\"0\" />" +
                        "</span>&nbsp;", sml.ImageFileName));
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("RetrieveError".ToLocalizedString(), "Smilie", ex.Message), ex) { EntityType = typeof(Smilie) };
            }
            return sb.ToString();
        }

        public static string ReplaceSmiliesTextToImage(string p)
        {
            try
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    var query = (from sml in ctx.SmilieSet
                                 select sml);
                    foreach (Smilie sml in query)
                    {
                        p = p.Replace(sml.CodeSmilie, String.Format("<img src=\"{0}\" border=\"0\" alt=\"\" />", sml.ImageFileName));
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("RetrieveError".ToLocalizedString(), "Smilie", ex.Message), ex) { EntityType = typeof(Smilie) };
            }
            return p;
        }

        public static int CreateEntity(ShoutBox entity)
        {
            try
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    ShoutBox myMessage = new ShoutBox();
                    myMessage.UserNameAuthorShoutBox = entity.UserNameAuthorShoutBox;
                    myMessage.MessageShoutBox = entity.MessageShoutBox;

                    ctx.AddToShoutBoxSet(myMessage);
                    ctx.SaveChanges();
                    return myMessage.Id;
                }
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("InsertError".ToLocalizedString(), "ShoutBox", ex.Message), ex) { EntityType = typeof(ShoutBox) };
            }
        }

        public static ShoutBox GetEntity(int entityID)
        {
            throw new NotImplementedException();
        }
        public static void UpdateEntity(ShoutBox entity)
        {
            throw new NotImplementedException();
        }
        public static void DeleteEntity(int entityID)
        {
            throw new NotImplementedException();
        }
        public static ShoutBox[] GetEntities(string searchText)
        {
            throw new NotImplementedException();
        }
    }
}
